// pages/orderList/orderList.js
const http = require('../../utils/http')
const config = require('../../utils/config')
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sts: 0,
    list: [],
    current: 1,
    pages: 0,
    totals: []
  },

  /***
   * 状态点击事件
   */
  onStsTap(e) {
    console.log("点击了", Number(e.currentTarget.dataset.sts))
    this.setData({
      sts: Number(e.currentTarget.dataset.sts)
    })
    this.loadOrderData(Number(e.currentTarget.dataset.sts), 1)
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.status) {
      this.setData({
        sts: Number(options.status)
      })
      this.loadOrderData(Number(options.status), 1)
    } else {
      this.loadOrderData(0, 1)
    }
  },

  loadOrderData(sts, current) {
    var status = null;
    if (sts == 1)
      status = "待支付"
    else if (sts == 2)
      status = "待发货"
    else if (sts === 3)
      status = "待收货"
    else if (sts == 4)
      status = "已完成"
    else if (sts == 5)
      status = "退货"
    console.log("status", status)
    if (sts == 0) {
      var page = this
      var params = {
        url: '/order',
        method: "GET",
        callBack(res) {
          console.log(res);
          page.setData({
            list: res
          })
          var totals = []
          res.forEach((cart) => {
            totals.push(util.calcul(cart.carts))
          })
          page.setData({
            totals: totals
          })
        },
        errCallBack(res) {}
      }
      http.request(params)
    } else {
      var page = this
      var params = {
        url: '/order',
        method: "GET",
        data: {
          status: status
        },
        callBack(res) {
          console.log(res);
          page.setData({
            list: res
          })
          var totals = []
          res.forEach((cart) => {
            totals.push(util.calcul(cart.carts))
          })
          page.setData({
            totals: totals
          })
        },
        errCallBack(res) {}
      }
      http.request(params)
    }
  },

  onCancelOrder(e) {
    const id = e.currentTarget.dataset.ordernum
    console.log("id:", id)
    var page = this
    wx.showModal({
      title: '',
      content: '是否取消该订单？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          wx.showLoading()
          var params = {
            url: '/order?id=' + id,
            method: "DELETE",
            callBack(res) {
              console.log(res);
              page.loadOrderData(1, 1)
              wx.hideLoading()
            },
            errCallBack(res) {}
          }
          http.request(params)
        }
      }
    })
  },

  pay(e) {
    const id = e.currentTarget.dataset.ordernum
    wx.navigateTo({
      url: '../pay/index?orderId=' + id
    })
  },

  receiveGoods(e) {
    var page = this;
    const id = e.currentTarget.dataset.ordernum
    var params = {
      url: '/order',
      method: "PUT",
      data: {
        id: id,
        status: '已完成'
      },
      callBack(res) {
        console.log(res);
        page.loadOrderData(4, 4)
        wx.hideLoading()
      },
      errCallBack(res) {}
    }
    http.request(params)
  },

  backGoods(e) {
    var page = this;
    const id = e.currentTarget.dataset.ordernum
    var params = {
      url: '/order',
      method: "PUT",
      data: {
        id: id,
        status: '退货'
      },
      callBack(res) {
        console.log(res);
        page.loadOrderData(5, 5)
        wx.hideLoading()
      },
      errCallBack(res) {}
    }
    http.request(params)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})